﻿using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.AspNetCore.Identity;

namespace Devonline.Identity;

/// <summary>
/// 资源, 字符串类型的默认实现
/// </summary>
[Table("resource"), DisplayName("资源")]
public class Resource : Resource<string>, IIdentity, IParent, IEntitySet, IEntitySetWithCreate, IEntitySetWithCreateAndUpdate
{
    /// <summary>
    /// 父级资源
    /// </summary>
    public virtual Resource Parent { get; set; }
    /// <summary>
    /// 资源级别
    /// </summary>
    public virtual Level Level { get; set; }
    /// <summary>
    /// 子级资源
    /// </summary>
    [NotMapped]
    public virtual ICollection<Resource> Children { get; set; }
    /// <summary>
    /// 通用附件集合, NotMapped Attachments 用于记录实体对象上上传的附件
    /// </summary>
    [NotMapped]
    public virtual ICollection<Attachment> Attachments { get; set; }
}

/// <summary>
/// 资源的定义
/// </summary>
[Table("resource"), DisplayName("资源")]
public class Resource<TKey> : Identity<TKey>, IIdentity<TKey>, IParent<TKey>, IEntitySet<TKey>, IEntitySetWithCreate<TKey>, IEntitySetWithCreateAndUpdate<TKey> where TKey : IConvertible
{
    /// <summary>
    /// 父级资源编号
    /// </summary>
    [Column("parent_id"), DisplayName("父级资源编号"), MaxLength(36), Excel]
    public virtual TKey ParentId { get; set; }
    /// <summary>
    /// 资源所有者编号
    /// </summary>
    [Column("owner_id"), DisplayName("资源所有者"), MaxLength(36), Excel]
    public virtual TKey OwnerId { get; set; }
    /// <summary>
    /// 资源所有者类型 IdentityType 枚举值
    /// </summary>
    [Column("identity_type", TypeName = "VARCHAR(16)"), DisplayName("资源所有者类型"), DefaultValue(IdentityType.User), Excel]
    public virtual IdentityType IdentityType { get; set; }
    /// <summary>
    /// 资源类型 ResourceType 枚举类型的值
    /// </summary>
    [Column("resource_type", TypeName = "VARCHAR(16)"), DisplayName("资源类型"), DefaultValue(ResourceType.Page), Excel]
    public virtual ResourceType ResourceType { get; set; }
    /// <summary>
    /// 名称
    /// </summary>
    [Column("name"), Required, Unique, MaxLength(256), DisplayName("名称")]
    public override string Name { get; set; }
    /// <summary>
    /// 资源标题
    /// </summary>
    [Column("title"), DisplayName("资源标题"), MaxLength(36), Excel]
    public virtual string Title { get; set; }
    /// <summary>
    /// 资源保存的具体内容, 可能是密文
    /// </summary>
    [ProtectedPersonalData]
    [Column("content"), DisplayName("资源内容"), MaxLength(1024), Excel]
    public virtual string Content { get; set; }
    /// <summary>
    /// 资源定义的可访问级别 AccessLevel 枚举类型的值
    /// </summary>
    [Column("access_level", TypeName = "VARCHAR(16)"), DisplayName("访问级别"), DefaultValue(AccessLevel.Internal), Excel]
    public virtual AccessLevel AccessLevel { get; set; }
    /// <summary>
    /// 资源级别, 即资源定义的最低访问级别
    /// </summary>
    [Column("level_id"), DisplayName("资源级别"), MaxLength(36), Excel]
    public virtual TKey LevelId { get; set; }
}